package com.luojialong.onlineJudge.model.vo;

import cn.hutool.json.JSONUtil;
import com.luojialong.onlineJudge.judge.codesandbox.model.JudgeInfo;
import com.luojialong.onlineJudge.model.entity.QuestionSubmit;
import com.luojialong.onlineJudge.model.enums.JudgeInfoMessageEnum;
import com.luojialong.onlineJudge.model.enums.QuestionSubmitStatusEnum;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;


/**
 * 题目
 *
 * @TableName question
 */

@Data
public class QuestionSubmitVO implements Serializable {
    /**
     * id
     */
    private Long id;

    /**
     * 编程语言
     */
    private String language;


    private String code;


    /**
     * 判题信息(json对象)
     */
    private JudgeInfo judgeInfo;

    /**
     * 判题状态(0 - 待判题,1 - 判题中,2 - 成功,3 - 失败)
     */
    private String status;

    /**
     * 题目id
     */
    private Long questionId;

    /**
     * 创建用户 id
     */
    private Long userId;

    private UserVO userVO;

    private QuestionVO questionVO;

    /**
     * 创建时间
     */
    private Date createTime;

    private String codeStatus;

    /**
     * 更新时间
     */
    private Date updateTime;


    private static final long serialVersionUID = 1L;


    /**
     * 包装类转对象
     *
     * @param questionSubmitVO
     * @return
     */
    public static QuestionSubmit voToObj(QuestionSubmitVO questionSubmitVO) {
        if (questionSubmitVO == null) {
            return null;
        }
        QuestionSubmit questionSubmit = new QuestionSubmit();
        BeanUtils.copyProperties(questionSubmitVO, questionSubmit);
        JudgeInfo judgeInfoObj = questionSubmitVO.getJudgeInfo();
        if (judgeInfoObj != null) {
            questionSubmit.setJudgeInfo(JSONUtil.toJsonStr(judgeInfoObj));
        }
        if (questionSubmitVO.getCodeStatus() != null) {
            questionSubmit.setCodeStatus(JudgeInfoMessageEnum.getEnumByValue(questionSubmitVO.codeStatus).getText());
        }
        return questionSubmit;
    }

    /**
     * 对象转包装类
     *
     * @param questionSubmit
     * @return
     */
    public static QuestionSubmitVO objToVo(QuestionSubmit questionSubmit) {
        if (questionSubmit == null) {
            return null;
        }
        QuestionSubmitVO questionSubmitVO = new QuestionSubmitVO();
        BeanUtils.copyProperties(questionSubmit, questionSubmitVO);
        questionSubmitVO.setStatus(QuestionSubmitStatusEnum.getEnumByValue(questionSubmit.getStatus()).getText());
        String judgeInfoStr = questionSubmit.getJudgeInfo();
        questionSubmitVO.setJudgeInfo(JSONUtil.toBean(judgeInfoStr, JudgeInfo.class));
        if (questionSubmit.getCodeStatus() != null) {
            questionSubmitVO.setCodeStatus(JudgeInfoMessageEnum.getEnumByText(questionSubmit.getCodeStatus()).getValue());
        }else {
            questionSubmitVO.setCodeStatus(JudgeInfoMessageEnum.SYSTEM_ERROR.getValue());
        }
        return questionSubmitVO;
    }
}